//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "everywhere.h"
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Ceverywhere::Ceverywhere () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _pheverywhere = NULL;
#endif
        _ppeverywhere = new boost::shared_ptr<QuantLib::everywhere> (new QuantLib::everywhere ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Ceverywhere::Ceverywhere (boost::shared_ptr<QuantLib::everywhere>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_pheverywhere = NULL;
#endif
	_ppeverywhere = &childNative;
    
}
Cephei::QL::Math::Ceverywhere::Ceverywhere (QuantLib::everywhere& childNative, Object^ owner) 
{
#ifdef HANDLE
	_pheverywhere = NULL;
#endif
	_ppeverywhere = new boost::shared_ptr<QuantLib::everywhere> (&childNative);
    
    _everywhereOwner = owner;
    
}

Cephei::QL::Math::Ceverywhere::Ceverywhere (Ceverywhere^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_pheverywhere = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppeverywhere = new boost::shared_ptr<QuantLib::everywhere> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Ceverywhere::Ceverywhere (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_pheverywhere = NULL;
#endif
	if (!t->IsSubclassOf(Ceverywhere::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Ceverywhere::Ceverywhere (QuantLib::Handle<QuantLib::everywhere>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_pheverywhere = &childNative;
	_ppeverywhere = &static_cast<boost::shared_ptr<QuantLib::everywhere>>(childNative.currentLink());
    
    _everywhereOwner = owner;
}
Cephei::QL::Math::Ceverywhere::Ceverywhere (QuantLib::Handle<QuantLib::everywhere> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_pheverywhere = &childNative;
	_ppeverywhere = &static_cast<boost::shared_ptr<QuantLib::everywhere>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Ceverywhere::Ceverywhere (QuantLib::everywhere childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_pheverywhere = NULL;
#endif
	_ppeverywhere = new boost::shared_ptr<QuantLib::everywhere> (new QuantLib::everywhere (childNative));
    
}
#endif

Cephei::QL::Math::Ceverywhere::~Ceverywhere ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppeverywhere != NULL)
    {
	    delete _ppeverywhere;
        _ppeverywhere = NULL;
    }
}
Cephei::QL::Math::Ceverywhere::!Ceverywhere ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppeverywhere != NULL)
    {
	    delete _ppeverywhere;
    }
}
QuantLib::everywhere& Cephei::QL::Math::Ceverywhere::GetReference ()
{
    if (_ppeverywhere == NULL) throw gcnew NativeNullException ();
	return **_ppeverywhere;
}
boost::shared_ptr<QuantLib::everywhere>& Cephei::QL::Math::Ceverywhere::GetShared ()
{
    if (_ppeverywhere == NULL) throw gcnew NativeNullException ();
	return *_ppeverywhere;
}
QuantLib::everywhere* Cephei::QL::Math::Ceverywhere::GetPointer ()
{
    if (_ppeverywhere == NULL) throw gcnew NativeNullException ();
	return &**_ppeverywhere;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::everywhere>& Cephei::QL::Math::Ceverywhere::GetHandle ()
{
	if (_pheverywhere == NULL)
	{
		_pheverywhere = new Handle<QuantLib::everywhere> (*_ppeverywhere);
	}
	return *_pheverywhere;
}
#endif
bool Cephei::QL::Math::Ceverywhere::HasNative () 
{
	return (_ppeverywhere != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Ieverywhere^ Cephei::QL::Math::Ceverywhere_Factory::Create ()
{
    return gcnew Ceverywhere ();
}
